import { AudioType } from "../util/defineConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class audioManager extends cc.Component {

    @property(cc.AudioClip)
    bgMusic:cc.AudioClip = null
    @property(cc.AudioClip)
    card:cc.AudioClip = null
    @property(cc.AudioClip)
    card_dealing:cc.AudioClip = null
    @property(cc.AudioClip)
    change_color:cc.AudioClip = null
    @property(cc.AudioClip)
    press_button:cc.AudioClip = null
    @property(cc.AudioClip)
    snap:cc.AudioClip = null
    @property(cc.AudioClip)
    game_over:cc.AudioClip = null
    @property(cc.AudioClip)
    special_card:cc.AudioClip = null

    private cardID:number = null;
    private card_dealing_ID:number = null;
    private change_color_ID:number = null;
    private press_button_ID:number = null;
    private snapID:number = null;
    private game_over_ID:number = null;
    private special_card_ID:number = null;
    private isMute:boolean = false;     //是否静音



    onLoad(){
        cc.audioEngine.playMusic(this.bgMusic,true)
    }

    // 暂停背景音乐
    pauseBgMusic(){
        cc.audioEngine.pauseMusic()
        this.isMute = true
    }

    //播放背景音乐
    resumeBgMusic(){
        cc.audioEngine.resumeMusic()
        this.isMute = false
    }

    // 播放音效
    playEffect(type:AudioType){
        switch(type){
            case AudioType.card:
                 if(this.isMute) return
                 this.cardID = cc.audioEngine.playEffect(this.card,false);
                 break;
            case AudioType.card_dealing: if(this.isMute) return
                 this.card_dealing_ID = cc.audioEngine.playEffect(this.card_dealing,false);
                 break;
            case AudioType.change_color: if(this.isMute) return
                 this.change_color_ID = cc.audioEngine.playEffect(this.change_color,false);
                 break;
            case AudioType.press_button: if(this.isMute) return
                 this.press_button_ID = cc.audioEngine.playEffect(this.press_button,false);
                 break;
            case AudioType.snap: if(this.isMute) return
                 this.snapID = cc.audioEngine.playEffect(this.snap,false);
                 break;
            case AudioType.game_over: if(this.isMute) return
                 this.game_over_ID = cc.audioEngine.playEffect(this.game_over,false);
                 break;
            case AudioType.special_card: if(this.isMute) return
                 this.special_card_ID = cc.audioEngine.playEffect(this.special_card,false);
                 break;
        }
    }

     // 暂停音效
     pauseEffect(type:AudioType){
        switch(type){
            case AudioType.card:this.pauseEffectByID(this.cardID);break;
            case AudioType.card_dealing:this.pauseEffectByID(this.card_dealing_ID);break;
            case AudioType.change_color:this.pauseEffectByID(this.change_color_ID);break;
            case AudioType.press_button:this.pauseEffectByID(this.press_button_ID);break;
            case AudioType.snap:this.pauseEffectByID(this.snapID);break;
            case AudioType.game_over:this.pauseEffectByID(this.game_over_ID);break;
            case AudioType.special_card:this.pauseEffectByID(this.special_card_ID);break;
        }
    }

    private pauseEffectByID(id:number){
        if(this.isMute) return
        cc.audioEngine.pauseEffect(id)
    }

    public initAudio(isMute:boolean){
        this.isMute = isMute
        if(this.isMute){
            this.pauseBgMusic()
        }
    }
}
